package com.lvmama.pangolin.task;

import com.lvmama.pangolin.HBaseOper.*;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.common.RowkeyGenerator;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.conf.ConfigManager;
import com.lvmama.pangolin.entity.ProductGrade;
import com.lvmama.pangolin.entity.RelevanceProduct;
import com.lvmama.pangolin.entity.TicketCommodityGrade;
import com.lvmama.pangolin.entity.TicketProductGrade;
import com.lvmama.pangolin.entity.link.TicketCommodityLink;
import com.lvmama.pangolin.entity.lmm.LMMTicketCommodity;
import com.lvmama.pangolin.entity.lmm.LMMTicketProduct;
import com.lvmama.pangolin.entity.mt.MTTicketCommodityItem;
import com.lvmama.pangolin.entity.mt.MTTicketProductItem;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.qnr.QNRTicketProductItem;
import com.lvmama.pangolin.entity.tc.TCProductItem;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketProductItem;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketProductItem;
import com.lvmama.pangolin.enums.Company;
import com.lvmama.pangolin.lmm.service.LMMTicketCommodityService;
import com.lvmama.pangolin.lmm.service.LMMTicketProductService;
import com.lvmama.pangolin.mt.service.MTTicketCommodityItemsService;
import com.lvmama.pangolin.mt.service.MTTicketProductItemsService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityItemsService;
import com.lvmama.pangolin.qnr.service.QNRTicketProductItemsService;
import com.lvmama.pangolin.service.*;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCProductItemsService;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.tn.service.TNTicketProductItemsService;
import com.lvmama.pangolin.util.CommodityGradeUtil;
import com.lvmama.pangolin.xc.service.XCTicketCommodityItemsService;
import com.lvmama.pangolin.xc.service.XCTicketProductItemsService;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class TicketGradeTask {
	/**
	 * @author zhoutianqi
	 * @className TicketGradeTask.java
	 * @date 2017年7月10日 上午10:24:37
	 * @description 门票评分
	 */
	private Logger logger = Logger.getLogger(getClass());
	@Autowired
	private ConfigManager configManager;
	@Autowired
	private RelevanceProductService relevanceProductService;
	@Autowired
	private RelevanceCommodityService relevanceCommodityService;

	//注入商品对象
	@Autowired
	private TCCommodityItemsService tcCommodityItemsService;
	@Autowired
	private TNTicketCommodityItemsService tnCommodityItemsService;
	@Autowired
	private XCTicketCommodityItemsService xcCommodityItemsService;
	@Autowired
	private QNRTicketCommodityItemsService qnrCommodityItemsService;
	@Autowired
	private MTTicketCommodityItemsService mtCommodityItemsService;

	//注入产品对象
	@Autowired
	private LMMTicketCommodityService lmmCommodityItemsService;
	@Autowired
	private TCProductItemsService tcProductItemsService;
	@Autowired
	private TNTicketProductItemsService tnProductItemsService;
	@Autowired
	private XCTicketProductItemsService xcProductItemsService;
	@Autowired
	private QNRTicketProductItemsService qnrProductItemsService;
	@Autowired
	private MTTicketProductItemsService mtProductItemsService;
	@Autowired
	private LMMTicketProductService lmmProductItemsService;

	//hbase数据库对象
	@Autowired
	private LMMHBEntityOperation lmmHbase;
	@Autowired
	private QNRHBEntityOperation qnrHbase;
	@Autowired
	private TCHBEntityOperation tcHbase;
	@Autowired
	private TNHBEntityOperation tnHbase;
	@Autowired
	private XCHBEntityOperation xcHbase;
	@Autowired
	private MTHBEntityOperation mtHbase;

	//打分对象
	@Autowired
	private ProductGradeService productGradeService;
	@Autowired
	private TicketCommodityGradeService ticketCommodityGradeService;
	@Autowired
	private TicketProductGradeService ticketProductGradeService;
	
	private static List<RelevanceProduct> relevanceProductList = new ArrayList<>();
	
	@RequestMapping("/startGradeTask")
	public String startGradeTask(){
		scheduledMothod();
		return "ok";
	}

	@Scheduled(cron = "${TICKET_GRADE_CRON}")
	public void scheduledMothod() {
		if (relevanceProductList.size() > 0) {
			return;
		} // 线程中任务处理完，再缓存任务
		relevanceProductList.clear();
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("businessCode", Constant.TICKE_BUS_TYPE);
		relevanceProductList = relevanceProductService.findByMap(paramMap);
		//relevanceProductList.add(relevanceProductService.viewRelevance("622407",Constant.TICKE_BUS_TYPE));
		//relevanceProductList.add(relevanceProductService.viewRelevance("237562",Constant.TICKE_BUS_TYPE));
		//relevanceProductList.add(relevanceProductService.viewRelevance("233203",Constant.TICKE_BUS_TYPE));
		/*relevanceProductList.add(relevanceProductService.viewRelevance("183237",Constant.TICKE_BUS_TYPE));
		relevanceProductList.add(relevanceProductService.viewRelevance("285579",Constant.TICKE_BUS_TYPE));
		relevanceProductList.add(relevanceProductService.viewRelevance("242667",Constant.TICKE_BUS_TYPE));*/
		logger.info("门票评分流程 Starting>>>");
		executeRelevanceProduct();
	}

	/**
	 * 逻辑:通过产品关联表，查找关联关系，校验相关产品，然后通过相应id查到竞品信息，数据处理
	 * 保存数据库，通过竞品比较得到相关商品比较分，入库，再22比较得产品分数，入库
	 */
	private void executeRelevanceProduct() {
		Date yesterday = DateUtil.getDateBeforeToday(new Date());
		//产品总得分
		double lmmProductTotalGrade = 0;
		double qnrProductTotalGrade = 0;
		double xcProductTotalGrade = 0;
		double tcProductTotalGrade = 0;
		double tnProductTotalGrade = 0;
		double mtProductTotalGrade = 0;
		int lmmProductNum = 0;
		int lmmFailedProductNum = 0;
		int qnrProductNum = 0;
		int xcProductNum = 0;
		int tcProductNum = 0;
		int tnProductNum = 0;
		int mtProductNum = 0;
		// 循环遍历已关联产品，计算产品评分
		for (RelevanceProduct item : relevanceProductList) {
			//满足运营产品两两评分报表统计需求start
			TicketProductGrade lmmQnrTpg = new TicketProductGrade();//统计跟去哪比较后驴妈妈的信息
			TicketProductGrade lmmTcTpg = new TicketProductGrade();
			TicketProductGrade lmmXcTpg = new TicketProductGrade();
			TicketProductGrade lmmTnTpg = new TicketProductGrade();
			TicketProductGrade lmmMtTpg = new TicketProductGrade();
			TicketProductGrade qnrTpg = new TicketProductGrade();
			TicketProductGrade tcTpg = new TicketProductGrade();
			TicketProductGrade xcTpg = new TicketProductGrade();
			TicketProductGrade tnTpg = new TicketProductGrade();
			TicketProductGrade mtTpg = new TicketProductGrade();//美团关联商品
			//满足运营产品两两评分报表统计需求end
			
			//在for里面try……catch  某个产品异常不会挂掉整个list
			try {
				// 由关联关系直接获取驴妈妈产品，lmm产品不存在的话…… 不可能！
				LMMTicketProduct LMMProduct = lmmProductItemsService.selectByProductId(item.getProdId());
				// lmm不存在，不用计算了
				if (LMMProduct == null) {
					lmmFailedProductNum++;
					throw new Exception("驴妈妈产品不存在");
				}
				//图片量不能为空
				if(!StringTools.isValid(LMMProduct.getImageNum())){
					LMMProduct.setImageNum("0");
				}
				// 竞品公司产品
				QNRTicketProductItem qnrProduct = null;
				TNTicketProductItem tnProduct = null;
				XCTicketProductItem xcProduct = null;
				TCProductItem tcProduct = null;
				MTTicketProductItem mtProduct = null;
				// 来五个商品实例，用来存储纵向产品级别中间值，所以该实例要嘛是通过商品关联关系获取的真实商品；要嘛new一个，不能是null
				LMMTicketCommodity lmmCommItem = new LMMTicketCommodity();
				XCTicketCommodityItem xcCommItem = new XCTicketCommodityItem();
				TCommodityItem tcCommItem = new TCommodityItem();
				TNTicketCommodityItem tnCommItem = new TNTicketCommodityItem();
				QNRTicketCommodityItem qnrCommItem = new QNRTicketCommodityItem();
				MTTicketCommodityItem mtCommItem = new MTTicketCommodityItem();
				if (StringTools.isValid(item.getProdIdQNR())) {
					qnrProduct = qnrProductItemsService.selectById(item.getProdIdQNR());
					//图片量不能为空
					if(qnrProduct!=null){
						if(!StringTools.isValid(qnrProduct.getImage_num())){
							qnrProduct.setImage_num("0");
						}
						//评分为第二天凌晨计算，我们只计算昨天抓到的数据
						if(!DateUtil.isSameDate(qnrProduct.getNew_grab_time(), yesterday)){
							qnrProduct=null;
						}
					}
				}
				if (StringTools.isValid(item.getProdIdTN())) {
					tnProduct = tnProductItemsService.selectById(item.getProdIdTN());
					//图片量不能为空
					if(tnProduct!=null){
						//评分为第二天凌晨计算，我们只计算昨天抓到的数据
						if(!StringTools.isValid(tnProduct.getImage_num())){
							tnProduct.setImage_num("0");
						}
						//评分为第二天凌晨计算，我们只计算昨天抓到的数据
						if(!DateUtil.isSameDate(tnProduct.getNew_grab_time(), yesterday)){
							tnProduct=null;
						}
					}
				}
				if (StringTools.isValid(item.getProdIdXC())) {
					xcProduct = xcProductItemsService.selectById(item.getProdIdXC());
					//图片量不能为空
					if(xcProduct!=null){
						if(!StringTools.isValid(xcProduct.getImage_num())){
							xcProduct.setImage_num("0");
						}
						//评分为第二天凌晨计算，我们只计算昨天抓到的数据
						if(!DateUtil.isSameDate(xcProduct.getNew_grab_time(), yesterday)){
							xcProduct=null;
						}
					}
				}
				if (StringTools.isValid(item.getProdIdTC())) {
					tcProduct = tcProductItemsService.selectById(item.getProdIdTC());
					//图片量不能为空
					if(tcProduct!=null){
						if(!StringTools.isValid(tcProduct.getImage_num())){
							tcProduct.setImage_num("0");
						}
						
						//评分为第二天凌晨计算，我们只计算昨天抓到的数据
						if(!DateUtil.isSameDate(tcProduct.getNew_grab_time(), yesterday)){
							tcProduct=null;
						}
					}
				}
				if (StringTools.isValid(item.getProdIdMT())) {
					mtProduct = mtProductItemsService.selectById(item.getProdIdMT());
					//图片量不能为空
					if(mtProduct!=null){
						if(!StringTools.isValid(mtProduct.getImage_num())){
							mtProduct.setImage_num("0");
						}

					//评分为第二天凌晨计算，我们只计算昨天抓到的数据
						if(!DateUtil.isSameDate(mtProduct.getNew_grab_time(), yesterday)){
							mtProduct=null;
						}
					}
				}
				//如果竞品都没有抓到 下一个
				if(qnrProduct==null && tnProduct==null && tcProduct==null && xcProduct==null && mtProduct==null){
					logger.info("lvmama产品"+LMMProduct.getProductId()+"关联产品对象均无效！");
					continue;
				}
				// 产品关联是行级关联，商品关联是列级关联
				// 产品下可能存在多个商品的关联，这里通过分组拿到所有建立关联的lmm商品ID
				List<String> commodityIdList = relevanceCommodityService.groupByCommodityId(item.getProdId(),Constant.TICKE_BUS_TYPE);
				// 统计产品下被建立关联的商品数量，lmm是主人，所以最多
				int lmmTimes = commodityIdList.size();
				int tcTimes = 0;
				int tnTimes = 0;
				int xcTimes = 0;
				int qnrTimes = 0;
				int mtTimes = 0;
				// 遍历所有关联商品，计算每个商品评分
				for (String commodityId : commodityIdList) {
					TicketCommodityLink param = new TicketCommodityLink();
					param.setProd_id(item.getProdId());
					param.setCommodity_id(commodityId);
					// 获取商品关联关系
					List<TicketCommodityLink> commodityLinkList = relevanceCommodityService.viewCommodityLink(param);
					// 记录lmm当前商品的关联数量tc tn xc qnr，用来计算两两比较后的均值
					double linkSize = Double.parseDouble(String.valueOf(commodityLinkList.size()));
					if (commodityLinkList != null && commodityLinkList.size() > 0) {
						// lmm商品必须存在，不然没得玩啊
						LMMTicketCommodity lmmComm = lmmCommodityItemsService.selectByProdCommId(
								commodityLinkList.get(0).getProd_id(), commodityLinkList.get(0).getCommodity_id());
						if (lmmComm == null) {
							//驴妈妈商品不存在时，只能continue不能抛异常，异常捕获后跳出当前产品校验
							lmmTimes--;
							continue;
						}
						// 存储横向商品级别中间变量对象不能是null
						XCTicketCommodityItem xcComm = new XCTicketCommodityItem();
						TCommodityItem tcComm = new TCommodityItem();
						TNTicketCommodityItem tnComm = new TNTicketCommodityItem();
						QNRTicketCommodityItem qnrComm = new QNRTicketCommodityItem();
						MTTicketCommodityItem mtComm = new MTTicketCommodityItem();
						// 根据商品关联信息获取商品
						for (TicketCommodityLink itemLink : commodityLinkList) {
							if (itemLink.getCompany_code().equals("tc")) {
								TCommodityItem tcComm1 = tcCommodityItemsService
										.selectByProdCommId(itemLink.getRel_prod_id(), itemLink.getRel_commodity_id());
								// 当关联关系中的某个商品不存在时，不做比较，关联数量减一
								if (tcComm1 != null && DateUtil.isSameDate(tcComm1.getNew_grab_time(),yesterday)) {
									tcComm = tcComm1;
									continue;
								}
								// tcComm1为null,说明关联关系存在，但该商品数据缺失，所以对比对象数量减一
								linkSize--;
							} else if (itemLink.getCompany_code().equals("tn")) {
								TNTicketCommodityItem tnComm1 = tnCommodityItemsService
										.selectByProdCommId(itemLink.getRel_prod_id(), itemLink.getRel_commodity_id());
								if (tnComm1 != null && DateUtil.isSameDate(tnComm1.getNew_grab_time(),yesterday)) {
									tnComm = tnComm1;
									continue;
								}
								linkSize--;
							} else if (itemLink.getCompany_code().equals("xc")) {
								XCTicketCommodityItem xcComm1 = xcCommodityItemsService
										.selectByProdCommId(itemLink.getRel_prod_id(), itemLink.getRel_commodity_id());
								if (xcComm1 != null && DateUtil.isSameDate(xcComm1.getNew_grab_time(),yesterday)) {
									xcComm = xcComm1;
									continue;
								}
								linkSize--;
							} else if (itemLink.getCompany_code().equals("qnr")) {
								QNRTicketCommodityItem qnrComm1 = qnrCommodityItemsService
										.selectByProdCommId(itemLink.getRel_prod_id(), itemLink.getRel_commodity_id());
								if (qnrComm1 != null && DateUtil.isSameDate(qnrComm1.getNew_grab_time(),yesterday)) {
									qnrComm = qnrComm1;
									continue;
								}
								linkSize--;
							}else if (itemLink.getCompany_code().equals("mt")) {
								MTTicketCommodityItem mtComm1 = mtCommodityItemsService
										.selectByProdCommId(itemLink.getRel_prod_id(), itemLink.getRel_commodity_id());

								if (mtComm1 != null && DateUtil.isSameDate(mtComm1.getNew_grab_time(),yesterday)) {
									mtComm = mtComm1;
									continue;
								}
								linkSize--;
							}
						}
						//当驴妈妈所关联的商品均无效时返回false
						boolean allCommAvaliable = preHandelCommodityValiable(xcComm, tcComm, tnComm, qnrComm,mtComm,qnrProduct,tnProduct,tcProduct,xcProduct,
								mtProduct);
						if (!allCommAvaliable) {
							//避免出现竞品下面没有商品，驴妈妈计数异常情况
							lmmTimes--;
							continue;
						}
						// 优惠力度、退改规则、提前预定时间、生效时间等各维度数据预处理 start
						preHandelCommodity(configManager, lmmComm, xcComm, tcComm, tnComm, qnrComm,mtComm);
						
						// 保存商品评分相关数据，用于商品评分明细展示和报表导出
						saveTicketCommodityGrade(lmmComm, xcComm, tcComm, tnComm, qnrComm,mtComm);
						
						// 优惠力度、退改规则、提前预定时间、生效时间等各维度数据预处理 end
						// 商品级别各维度得分 start
						// 价格维度两两比较，大于得100分，等于得80分，小于得60分
						double lmmPriceValue = CommodityGradeUtil.compareValues2(lmmComm.getSalePrice(),
								xcComm.getSale_price(), tnComm.getSale_price(), tcComm.getSale_price(),
								qnrComm.getSale_price(),mtComm.getSale_price()) / linkSize;
						// 横向商品级别保存该维度得分，用于计算商品维度总得分
						lmmComm.setPriceGrade(lmmPriceValue);
						// 纵向产品级别保存该得分，用于计算产品级别在该维度的平均分
						lmmCommItem.setPriceGrade(lmmCommItem.getPriceGrade() + lmmPriceValue);
						// 退改规则级别评分
						double lmmrefundValue = CommodityGradeUtil.compareValues1(lmmComm.getRefundCondValue(),
								xcComm.getRefundCondValue(), tnComm.getRefundCondValue(), tcComm.getRefundCondValue(),
								qnrComm.getRefundCondValue(),mtComm.getRefundCondValue()) / linkSize;
						lmmComm.setRefundCondGrade(lmmrefundValue);
						lmmCommItem.setRefundCondGrade(lmmCommItem.getRefundCondGrade() + lmmrefundValue);
						// 优惠力度级别评分
						double lmmPreferentialValue = CommodityGradeUtil.compareValues1(lmmComm.getPreferentialValue(),
								xcComm.getPreferentialValue(), tnComm.getPreferentialValue(),
								tcComm.getPreferentialValue(), qnrComm.getPreferentialValue(),mtComm.getPreferentialValue()) / linkSize;
						lmmComm.setPreferentialGrade(lmmPreferentialValue);
						lmmCommItem.setPreferentialGrade(lmmCommItem.getPreferentialGrade() + lmmPreferentialValue);
						// 生效时间级别评分
						double lmmEfficientValue = CommodityGradeUtil.compareValues2(lmmComm.getEfficientValue(),
								xcComm.getEfficientValue(), tnComm.getEfficientValue(), tcComm.getEfficientValue(),
								qnrComm.getEfficientValue(),mtComm.getEfficientValue()) / linkSize;
						lmmComm.setEfficientGrade(lmmEfficientValue);
						lmmCommItem.setEfficientGrade(lmmCommItem.getEfficientGrade() + lmmEfficientValue);
						// 预定时间级别评分
						double lmmBooktimeValue = CommodityGradeUtil.compareValues2(lmmComm.getBooktimeValue(),
								xcComm.getBooktimeValue(), tnComm.getBooktimeValue(), tcComm.getBooktimeValue(),
								qnrComm.getBooktimeValue(),mtComm.getBooktimeValue()) / linkSize;
						lmmComm.setBooktimeGrade(lmmBooktimeValue);
						lmmCommItem.setBooktimeGrade(lmmCommItem.getBooktimeGrade() + lmmBooktimeValue);
						// 横向商品级别计算商品评分
						double commodityGrade = CommodityGradeUtil.getObjectGrade(configManager, 1, lmmPriceValue,
								lmmrefundValue, lmmPreferentialValue, lmmEfficientValue, lmmBooktimeValue);
						// 商品维度得分存储，并入HBase商品表
						lmmComm.setCommodityGrade(String.valueOf(commodityGrade));
						lmmHbase.saveLMMTicketCommodity(lmmComm, Constant.TICKET_COMMODITY_H_LMM,
								Constant.HB_VARIABLE_FAMILY);
						// 这边判断价格实际上也是判断当前商品是否有被关联，new的对象价格为null
						if (StringTools.isValid(xcComm.getSale_price()) && xcProduct!=null) {//避免产品不存在，却存在商品关联的情况
							xcTimes++;
							double xcpriceValue = CommodityGradeUtil.compareValues2(xcComm.getSale_price(),
									lmmComm.getSalePrice(), tnComm.getSale_price(), tcComm.getSale_price(),
									qnrComm.getSale_price(),mtComm.getSale_price()) / linkSize;
							xcComm.setPriceGrade(xcpriceValue);
							xcCommItem.setPriceGrade(xcCommItem.getPriceGrade() + xcpriceValue);
							double refundValue = CommodityGradeUtil.compareValues1(xcComm.getRefundCondValue(),
									lmmComm.getRefundCondValue(), tnComm.getRefundCondValue(),
									tcComm.getRefundCondValue(), qnrComm.getRefundCondValue(),mtComm.getRefundCondValue()) / linkSize;
							xcComm.setRefundCondGrade(refundValue);
							xcCommItem.setRefundCondGrade(xcCommItem.getRefundCondGrade() + refundValue);
							double preferentialValue = CommodityGradeUtil.compareValues1(xcComm.getPreferentialValue(),
									lmmComm.getPreferentialValue(), tnComm.getPreferentialValue(),
									tcComm.getPreferentialValue(), qnrComm.getPreferentialValue(),mtComm.getPreferentialValue()) / linkSize;
							xcComm.setPreferentialGrade(preferentialValue);
							xcCommItem.setPreferentialGrade(xcCommItem.getPreferentialGrade() + preferentialValue);
							double xcEfficientValue = CommodityGradeUtil.compareValues2(xcComm.getEfficientValue(),
									lmmComm.getEfficientValue(), tnComm.getEfficientValue(), tcComm.getEfficientValue(),
									qnrComm.getEfficientValue(),mtComm.getEfficientValue()) / linkSize;
							xcComm.setEfficientGrade(xcEfficientValue);
							xcCommItem.setEfficientGrade(xcCommItem.getEfficientGrade() + xcEfficientValue);
							double xcBooktimeValue = CommodityGradeUtil.compareValues2(xcComm.getBooktimeValue(),
									lmmComm.getBooktimeValue(), tnComm.getBooktimeValue(), tcComm.getBooktimeValue(),
									qnrComm.getBooktimeValue(),mtComm.getBooktimeValue()) / linkSize;
							xcComm.setBooktimeGrade(xcBooktimeValue);
							xcCommItem.setBooktimeGrade(xcCommItem.getBooktimeGrade() + xcBooktimeValue);
							// 计算商品评分
							double xcCommodityGrade = CommodityGradeUtil.getObjectGrade(configManager, 1, xcpriceValue,
									refundValue, preferentialValue, xcEfficientValue, xcBooktimeValue);
							xcComm.setCommodity_grade(String.valueOf(xcCommodityGrade));
							xcHbase.saveXCTicketCommodity(xcComm, Constant.TICKET_COMMODITY_H_XC,
									Constant.HB_VARIABLE_FAMILY);
							//满足运营产品两两评分报表统计需求start
							CommodityGradeUtil.xcTicketProductGrade(lmmXcTpg,xcTpg,lmmComm,xcComm);
							//满足运营产品两两评分报表统计需求end
						}
						if (StringTools.isValid(tcComm.getSale_price()) && tcProduct!=null) {//避免产品不存在，却存在商品关联的情况
							tcTimes++;
							double tcPriceValue = CommodityGradeUtil.compareValues2(tcComm.getSale_price(),
									lmmComm.getSalePrice(), tnComm.getSale_price(), xcComm.getSale_price(),
									qnrComm.getSale_price(),mtComm.getSale_price()) / linkSize;
							tcComm.setPriceGrade(tcPriceValue);
							tcCommItem.setPriceGrade(tcCommItem.getPriceGrade() + tcPriceValue);
							double tcrefundValue = CommodityGradeUtil.compareValues1(tcComm.getRefundCondValue(),
									lmmComm.getRefundCondValue(), tnComm.getRefundCondValue(),
									xcComm.getRefundCondValue(), qnrComm.getRefundCondValue(),mtComm.getRefundCondValue()) / linkSize;
							tcComm.setRefundCondGrade(tcrefundValue);
							tcCommItem.setRefundCondGrade(tcCommItem.getRefundCondGrade() + tcrefundValue);
							double preferentialValue = CommodityGradeUtil.compareValues1(tcComm.getPreferentialValue(),
									lmmComm.getPreferentialValue(), tnComm.getPreferentialValue(),
									xcComm.getPreferentialValue(), qnrComm.getPreferentialValue(),mtComm.getPreferentialValue()) / linkSize;
							tcComm.setPreferentialGrade(preferentialValue);
							tcCommItem.setPreferentialGrade(tcCommItem.getPreferentialGrade() + preferentialValue);
							double tcEfficientValue = CommodityGradeUtil.compareValues2(tcComm.getEfficientValue(),
									lmmComm.getEfficientValue(), xcComm.getEfficientValue(), tnComm.getEfficientValue(),
									qnrComm.getEfficientValue(),qnrComm.getEfficientValue()) / linkSize;
							tcComm.setEfficientGrade(tcEfficientValue);
							tcCommItem.setEfficientGrade(tcCommItem.getEfficientGrade() + tcEfficientValue);
							double tcBooktimeValue = CommodityGradeUtil.compareValues2(tcComm.getBooktimeValue(),
									lmmComm.getBooktimeValue(), tnComm.getBooktimeValue(), xcComm.getBooktimeValue(),
									qnrComm.getBooktimeValue(),mtComm.getBooktimeValue()) / linkSize;
							tcComm.setBooktimeGrade(tcBooktimeValue);
							tcCommItem.setBooktimeGrade(tcCommItem.getBooktimeGrade() + tcBooktimeValue);
							// 计算商品评分
							double tcCommodityGrade = CommodityGradeUtil.getObjectGrade(configManager, 1, tcPriceValue,
									tcrefundValue, preferentialValue, tcEfficientValue, tcBooktimeValue);
							tcComm.setCommodity_grade(String.valueOf(tcCommodityGrade));
							tcHbase.saveTCTicketCommodity(tcComm, Constant.TICKET_COMMODITY_H_TC,
									Constant.HB_VARIABLE_FAMILY);
							//满足运营产品两两评分报表统计需求start
							CommodityGradeUtil.tcTicketProductGrade(lmmTcTpg,tcTpg,lmmComm,tcComm);
							//满足运营产品两两评分报表统计需求end
						}
						if (StringTools.isValid(tnComm.getSale_price()) && tnProduct!=null) {//避免产品不存在，却存在商品关联的情况
							tnTimes++;
							double tnPriceValue = CommodityGradeUtil.compareValues2(tnComm.getSale_price(),
									lmmComm.getSalePrice(), xcComm.getSale_price(), tcComm.getSale_price(),
									qnrComm.getSale_price(),mtComm.getSale_price()) / linkSize;
							tnComm.setPriceGrade(tnPriceValue);
							tnCommItem.setPriceGrade(tnCommItem.getPriceGrade() + tnPriceValue);
							double tnrefundValue = CommodityGradeUtil.compareValues1(tnComm.getRefundCondValue(),
									lmmComm.getRefundCondValue(), xcComm.getRefundCondValue(),
									tcComm.getRefundCondValue(), qnrComm.getRefundCondValue(),mtComm.getRefundCondValue()) / linkSize;
							tnComm.setRefundCondGrade(tnrefundValue);
							tnCommItem.setRefundCondGrade(tnCommItem.getRefundCondGrade() + tnrefundValue);
							double preferentialValue = CommodityGradeUtil.compareValues1(tnComm.getPreferentialValue(),
									lmmComm.getPreferentialValue(), xcComm.getPreferentialValue(),
									tcComm.getPreferentialValue(), qnrComm.getPreferentialValue(),mtComm.getPreferentialValue()) / linkSize;
							tnComm.setPreferentialGrade(preferentialValue);
							tnCommItem.setPreferentialGrade(tnCommItem.getPreferentialGrade() + preferentialValue);
							double tnEfficientValue = CommodityGradeUtil.compareValues2(tnComm.getEfficientValue(),
									lmmComm.getEfficientValue(), xcComm.getEfficientValue(), tcComm.getEfficientValue(),
									qnrComm.getEfficientValue(),mtComm.getEfficientValue()) / linkSize;
							tnComm.setEfficientGrade(tnEfficientValue);
							tnCommItem.setEfficientGrade(tnCommItem.getEfficientGrade() + tnEfficientValue);
							double tnBooktimeValue = CommodityGradeUtil.compareValues2(tnComm.getBooktimeValue(),
									lmmComm.getBooktimeValue(), tcComm.getBooktimeValue(), xcComm.getBooktimeValue(),
									qnrComm.getBooktimeValue(),mtComm.getBooktimeValue()) / linkSize;
							tnComm.setBooktimeGrade(tnBooktimeValue);
							tnCommItem.setBooktimeGrade(tnCommItem.getBooktimeGrade() + tnBooktimeValue);
							// 计算商品评分
							double tnCommodityGrade = CommodityGradeUtil.getObjectGrade(configManager, 1, tnPriceValue,
									tnrefundValue, preferentialValue, tnEfficientValue, tnBooktimeValue);
							tnComm.setCommodity_grade(String.valueOf(tnCommodityGrade));
							tnHbase.saveTNTicketCommodity(tnComm, Constant.TICKET_COMMODITY_H_TN,
									Constant.HB_VARIABLE_FAMILY);
							//满足运营产品两两评分报表统计需求start
							CommodityGradeUtil.tnTicketProductGrade(lmmTnTpg,tnTpg,lmmComm,tnComm);
							//满足运营产品两两评分报表统计需求end
						}
						if (StringTools.isValid(qnrComm.getSale_price()) && qnrProduct!=null) {//避免产品不存在，却存在商品关联的情况
							qnrTimes++;
							double qnrPriceValue = CommodityGradeUtil.compareValues2(qnrComm.getSale_price(),
									lmmComm.getSalePrice(), tnComm.getSale_price(), tcComm.getSale_price(),
									xcComm.getSale_price(),mtComm.getSale_price()) / linkSize;
							qnrComm.setPriceGrade(qnrPriceValue);
							qnrCommItem.setPriceGrade(qnrCommItem.getPriceGrade() + qnrPriceValue);
							double qnrrefundValue = CommodityGradeUtil.compareValues1(qnrComm.getRefundCondValue(),
									lmmComm.getRefundCondValue(), tnComm.getRefundCondValue(),
									tcComm.getRefundCondValue(), xcComm.getRefundCondValue(),mtComm.getRefundCondValue()) / linkSize;
							qnrComm.setRefundCondGrade(qnrrefundValue);
							qnrCommItem.setRefundCondGrade(qnrCommItem.getRefundCondGrade() + qnrrefundValue);
							double preferentialValue = CommodityGradeUtil.compareValues1(qnrComm.getPreferentialValue(),
									lmmComm.getPreferentialValue(), tnComm.getPreferentialValue(),
									tcComm.getPreferentialValue(), xcComm.getPreferentialValue(), mtComm.getPreferentialValue()) / linkSize;
							qnrComm.setPreferentialGrade(preferentialValue);
							qnrCommItem.setPreferentialGrade(qnrCommItem.getPreferentialGrade() + preferentialValue);
							double qnrEfficientValue = CommodityGradeUtil.compareValues2(qnrComm.getEfficientValue(),
									lmmComm.getEfficientValue(), tnComm.getEfficientValue(), tcComm.getEfficientValue(),
									xcComm.getEfficientValue(),mtComm.getEfficientValue()) / linkSize;
							qnrComm.setEfficientGrade(qnrEfficientValue);
							qnrCommItem.setEfficientGrade(qnrCommItem.getEfficientGrade() + qnrEfficientValue);
							double qnrBooktimeValue = CommodityGradeUtil.compareValues2(qnrComm.getBooktimeValue(),
									lmmComm.getBooktimeValue(), tcComm.getBooktimeValue(), xcComm.getBooktimeValue(),
									tnComm.getBooktimeValue(),mtComm.getBooktimeValue()) / linkSize;
							qnrComm.setBooktimeGrade(qnrBooktimeValue);
							qnrCommItem.setBooktimeGrade(qnrCommItem.getBooktimeGrade() + qnrBooktimeValue);
							// 计算商品评分
							double qnrCommodityGrade = CommodityGradeUtil.getObjectGrade(configManager, 1,
									qnrPriceValue, qnrrefundValue, preferentialValue, qnrEfficientValue,
									qnrBooktimeValue);
							qnrComm.setCommodity_grade(String.valueOf(qnrCommodityGrade));
							qnrHbase.saveQNRTicketCommodity(qnrComm, Constant.TICKET_COMMODITY_H_QNR,
									Constant.HB_VARIABLE_FAMILY);
							//满足运营产品两两评分报表统计需求start
							CommodityGradeUtil.qnrTicketProductGrade(lmmQnrTpg,qnrTpg,lmmComm,qnrComm);
							//满足运营产品两两评分报表统计需求end
						}
						if (StringTools.isValid(mtComm.getSale_price()) && mtProduct!=null) {//避免产品不存在，却存在商品关联的情况
							mtTimes++;
							double mtPriceValue = CommodityGradeUtil.compareValues2(mtComm.getSale_price(),
									lmmComm.getSalePrice(), tnComm.getSale_price(), tcComm.getSale_price(),
									xcComm.getSale_price(),qnrComm.getSale_price()) / linkSize;
							mtComm.setPriceGrade(mtPriceValue);
							mtCommItem.setPriceGrade(mtCommItem.getPriceGrade() + mtPriceValue);
							double mtrefundValue = CommodityGradeUtil.compareValues1(mtComm.getRefundCondValue(),
									lmmComm.getRefundCondValue(), tnComm.getRefundCondValue(),
									tcComm.getRefundCondValue(), xcComm.getRefundCondValue(),qnrComm.getRefundCondValue()) / linkSize;
							mtComm.setRefundCondGrade(mtrefundValue);
							mtCommItem.setRefundCondGrade(mtCommItem.getRefundCondGrade() + mtrefundValue);
							double preferentialValue = CommodityGradeUtil.compareValues1(mtComm.getPreferentialValue(),
									lmmComm.getPreferentialValue(), tnComm.getPreferentialValue(),
									tcComm.getPreferentialValue(), xcComm.getPreferentialValue(), qnrComm.getPreferentialValue()) / linkSize;
							mtComm.setPreferentialGrade(preferentialValue);
							mtCommItem.setPreferentialGrade(mtCommItem.getPreferentialGrade() + preferentialValue);
							double mtEfficientValue = CommodityGradeUtil.compareValues2(mtComm.getEfficientValue(),
									lmmComm.getEfficientValue(), tnComm.getEfficientValue(), tcComm.getEfficientValue(),
									xcComm.getEfficientValue(),qnrComm.getEfficientValue()) / linkSize;
							mtComm.setEfficientGrade(mtEfficientValue);
							mtCommItem.setEfficientGrade(mtCommItem.getEfficientGrade() + mtEfficientValue);
							double mtBooktimeValue = CommodityGradeUtil.compareValues2(mtComm.getBooktimeValue(),
									lmmComm.getBooktimeValue(), tcComm.getBooktimeValue(), xcComm.getBooktimeValue(),
									tnComm.getBooktimeValue(),qnrComm.getBooktimeValue()) / linkSize;
							mtComm.setBooktimeGrade(mtBooktimeValue);
							mtCommItem.setBooktimeGrade(mtCommItem.getBooktimeGrade() + mtBooktimeValue);
							// 计算商品评分
							double mtCommodityGrade = CommodityGradeUtil.getObjectGrade(configManager, 1,
									mtPriceValue, mtrefundValue, preferentialValue, mtEfficientValue,
									mtBooktimeValue);
							mtComm.setCommodity_grade(String.valueOf(mtCommodityGrade));
							mtHbase.saveMTTicketCommodity(mtComm, Constant.TICKET_COMMODITY_H_MT,
									Constant.HB_VARIABLE_FAMILY);
							//满足运营产品两两评分报表统计需求start
							CommodityGradeUtil.mtTicketProductGrade(lmmMtTpg,mtTpg,lmmComm,mtComm);
							//满足运营产品两两评分报表统计需求end
						}
						// 商品级别计算价格得分 end
					}
				}

				// 产品级别评分计算、更新产品评分 start
				// 判断xxTimes来判断对应竞品公司在该产品上是否存在关联的商品
				int qnrCommodityNum = qnrProduct==null?0:qnrProduct.getCommodity_num();
				int tcCommodityNum = tcProduct==null?0:tcProduct.getCommodity_num();
				int tnCommodityNum = tnProduct==null?0:tnProduct.getCommodity_num();
				int xcCommodityNum = xcProduct==null?0:xcProduct.getCommodity_num();
				int mtCommodityNum = mtProduct==null?0:mtProduct.getCommodity_num();
				if (lmmTimes > 0) {
					double commodityGrade1 = CommodityGradeUtil.getObjectGrade(configManager, lmmTimes,
							lmmCommItem.getPriceGrade(), lmmCommItem.getRefundCondGrade(),
							lmmCommItem.getPreferentialGrade(), lmmCommItem.getEfficientGrade(),
							lmmCommItem.getBooktimeGrade());
					double productGrade = CommodityGradeUtil.getProductGrade(configManager, commodityGrade1, LMMProduct.getCommodityNum(), Double.parseDouble(LMMProduct.getImageNum()), LMMProduct.getScore(), "LMM");
					LMMProduct.setProductGrade(StringTools.formatDouble2(productGrade));
					LMMProduct.setPriceGrade(String.valueOf(lmmCommItem.getPriceGrade() / lmmTimes));
					LMMProduct.setRefundCondGrade(String.valueOf(lmmCommItem.getRefundCondGrade() / lmmTimes));
					LMMProduct.setPreferentialGrade(String.valueOf(lmmCommItem.getPreferentialGrade() / lmmTimes));
					LMMProduct.setEfficientGrade(String.valueOf(lmmCommItem.getEfficientGrade() / lmmTimes));
					LMMProduct.setBooktimeGrade(String.valueOf(lmmCommItem.getBooktimeGrade() / lmmTimes));
					lmmProductItemsService.updateByProdIdSelective(LMMProduct);
					String currenrKey = RowkeyGenerator.generateRowkey(LMMProduct.getProductId(), LMMProduct.getNewGrabTime(), LMMProduct.getTaskId());
					LMMProduct.setId(currenrKey);
					lmmHbase.saveLMMTicketProduct(LMMProduct, Constant.TICKET_PRODUCT_H_LMM,
							Constant.HB_VARIABLE_FAMILY);
					//累加产品评分
					lmmProductTotalGrade+=productGrade;
					//参与评分计算的产品数累加
					lmmProductNum++;
				}else{
					lmmFailedProductNum++;
				}
				// 判断xxTimes来判断对应竞品公司在该产品上是否存在关联的商品
				if (tcTimes > 0) {
					double commodityGrade2 = CommodityGradeUtil.getObjectGrade(configManager, tcTimes,
							tcCommItem.getPriceGrade(), tcCommItem.getRefundCondGrade(),
							tcCommItem.getPreferentialGrade(), tcCommItem.getEfficientGrade(),
							tcCommItem.getBooktimeGrade());
					double productGrade = CommodityGradeUtil.getProductGrade(configManager, commodityGrade2, tcProduct.getCommodity_num(), Double.parseDouble(tcProduct.getImage_num()), tcProduct.getScore(), "TC");
					tcProduct.setProduct_grade(StringTools.formatDouble2(productGrade));
					tcProduct.setPriceGrade(String.valueOf(tcCommItem.getPriceGrade() / tcTimes));
					tcProduct.setRefundCondGrade(String.valueOf(tcCommItem.getRefundCondGrade() / tcTimes));
					tcProduct.setPreferentialGrade(String.valueOf(tcCommItem.getPreferentialGrade() / tcTimes));
					tcProduct.setEfficientGrade(String.valueOf(tcCommItem.getEfficientGrade() / tcTimes));
					tcProduct.setBooktimeGrade(String.valueOf(tcCommItem.getBooktimeGrade() / tcTimes));
					tcProductItemsService.updateByItem(tcProduct);
					String currenrKey = RowkeyGenerator.generateRowkey(tcProduct.getProduct_id(), tcProduct.getNew_grab_time(), tcProduct.getTask_id());
					tcProduct.setId(currenrKey);
					tcHbase.saveTCTicketProduct(tcProduct, Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
					//累加产品评分
					tcProductTotalGrade+=productGrade;
					//参与评分计算的产品数累加
					tcProductNum++;
					//满足运营产品两两评分报表统计需求start
					saveProductGrade(lmmTcTpg,Company.LMMTC.getCode(),LMMProduct.getCommodityNum(),LMMProduct.getProductId()
							,LMMProduct.getTaskId(),tcTpg,Company.TC.getCode(),
							tcCommodityNum,tcProduct.getProduct_id(),tcProduct.getTask_id());
					//满足运营产品两两评分报表统计需求end
				}
				// 判断xxTimes来判断对应竞品公司在该产品上是否存在关联的商品
				if (tnTimes > 0) {
					double commodityGrade3 = CommodityGradeUtil.getObjectGrade(configManager, tnTimes,
							tnCommItem.getPriceGrade(), tnCommItem.getRefundCondGrade(),
							tnCommItem.getPreferentialGrade(), tnCommItem.getEfficientGrade(),
							tnCommItem.getBooktimeGrade());
					double productGrade = CommodityGradeUtil.getProductGrade(configManager, commodityGrade3, tnProduct.getCommodity_num(), Double.parseDouble(tnProduct.getImage_num()), tnProduct.getScore(), "TN");
					tnProduct.setProduct_grade(StringTools.formatDouble2(productGrade));
					tnProduct.setPriceGrade(String.valueOf(tnCommItem.getPriceGrade() / tnTimes));
					tnProduct.setRefundCondGrade(String.valueOf(tnCommItem.getRefundCondGrade() / tnTimes));
					tnProduct.setPreferentialGrade(String.valueOf(tnCommItem.getPreferentialGrade() / tnTimes));
					tnProduct.setEfficientGrade(String.valueOf(tnCommItem.getEfficientGrade() / tnTimes));
					tnProduct.setBooktimeGrade(String.valueOf(tnCommItem.getBooktimeGrade() / tnTimes));
					tnProductItemsService.updateByItem(tnProduct);
					String currenrKey = RowkeyGenerator.generateRowkey(tnProduct.getProduct_id(), tnProduct.getNew_grab_time(), tnProduct.getTask_id());
					tnProduct.setId(currenrKey);
					tnHbase.saveTNTicketProduct(tnProduct, Constant.TICKET_PRODUCT_H_TN, Constant.HB_VARIABLE_FAMILY);
					//累加产品评分
					tnProductTotalGrade+=productGrade;
					//参与评分计算的产品数累加
					tnProductNum++;
					
					// 保存途牛产品评分
					//满足运营产品两两评分报表统计需求start
					saveProductGrade(lmmTnTpg,Company.LMMTN.getCode(),LMMProduct.getCommodityNum(),LMMProduct.getProductId()
							,LMMProduct.getTaskId(),tnTpg,Company.TN.getCode(),
							tnCommodityNum,tnProduct.getProduct_id(),tnProduct.getTask_id());
					//满足运营产品两两评分报表统计需求end
				}
				// 判断xxTimes来判断对应竞品公司在该产品上是否存在关联的商品
				if (xcTimes > 0) {
					double commodityGrade4 = CommodityGradeUtil.getObjectGrade(configManager, xcTimes,
							xcCommItem.getPriceGrade(), xcCommItem.getRefundCondGrade(),
							xcCommItem.getPreferentialGrade(), xcCommItem.getEfficientGrade(),
							xcCommItem.getBooktimeGrade());
					double productGrade = CommodityGradeUtil.getProductGrade(configManager, commodityGrade4, xcProduct.getCommodity_num(), Double.parseDouble(xcProduct.getImage_num()), xcProduct.getScore(), "XC");
					xcProduct.setProduct_grade(StringTools.formatDouble2(productGrade));
					xcProduct.setPriceGrade(String.valueOf(xcCommItem.getPriceGrade() / xcTimes));
					xcProduct.setRefundCondGrade(String.valueOf(xcCommItem.getRefundCondGrade() / xcTimes));
					xcProduct.setPreferentialGrade(String.valueOf(xcCommItem.getPreferentialGrade() / xcTimes));
					xcProduct.setEfficientGrade(String.valueOf(xcCommItem.getEfficientGrade() / xcTimes));
					xcProduct.setBooktimeGrade(String.valueOf(xcCommItem.getBooktimeGrade() / xcTimes));
					xcProductItemsService.updateByItem(xcProduct);
					String currenrKey = RowkeyGenerator.generateRowkey(xcProduct.getProduct_id(), xcProduct.getNew_grab_time(), xcProduct.getTask_id());
					xcProduct.setId(currenrKey);
					xcHbase.saveXCTicketProduct(xcProduct, Constant.TICKET_PRODUCT_H_XC, Constant.HB_VARIABLE_FAMILY);
					//累加产品评分
					xcProductTotalGrade+=productGrade;
					//参与评分计算的产品数累加
					xcProductNum++;
					

					// 保存携程产品评分
					//满足运营产品两两评分报表统计需求start
					saveProductGrade(lmmXcTpg,Company.LMMXC.getCode(),LMMProduct.getCommodityNum(),LMMProduct.getProductId()
							,LMMProduct.getTaskId(),xcTpg,Company.XC.getCode(),
							xcCommodityNum,xcProduct.getProduct_id(),xcProduct.getTask_id());
					//满足运营产品两两评分报表统计需求end
				}
				// 判断xxTimes来判断对应竞品公司在该产品上是否存在关联的商品
				if (qnrTimes > 0) {
					double commodityGrade5 = CommodityGradeUtil.getObjectGrade(configManager, qnrTimes,
							qnrCommItem.getPriceGrade(), qnrCommItem.getRefundCondGrade(),
							qnrCommItem.getPreferentialGrade(), qnrCommItem.getEfficientGrade(),
							qnrCommItem.getBooktimeGrade());
					double productGrade = CommodityGradeUtil.getProductGrade(configManager, commodityGrade5, qnrProduct.getCommodity_num(), Double.parseDouble(qnrProduct.getImage_num()), qnrProduct.getScore(), "QNR");
					qnrProduct.setProduct_grade(StringTools.formatDouble2(productGrade));
					qnrProduct.setPriceGrade(String.valueOf(qnrCommItem.getPriceGrade() / qnrTimes));
					qnrProduct.setRefundCondGrade(String.valueOf(qnrCommItem.getRefundCondGrade() / qnrTimes));
					qnrProduct.setPreferentialGrade(String.valueOf(qnrCommItem.getPreferentialGrade() / qnrTimes));
					qnrProduct.setEfficientGrade(String.valueOf(qnrCommItem.getEfficientGrade() / qnrTimes));
					qnrProduct.setBooktimeGrade(String.valueOf(qnrCommItem.getBooktimeGrade() / qnrTimes));
					qnrProductItemsService.updateByItem(qnrProduct);
					String currenrKey = RowkeyGenerator.generateRowkey(qnrProduct.getProduct_id(), qnrProduct.getNew_grab_time(), qnrProduct.getTask_id());
					qnrProduct.setId(currenrKey);
					qnrHbase.saveQNRTicketProduct(qnrProduct, Constant.TICKET_PRODUCT_H_QNR,
							Constant.HB_VARIABLE_FAMILY);
					//累加产品评分
					qnrProductTotalGrade+=productGrade;
					//参与评分计算的产品数累加
					qnrProductNum++;
					
					// 保存去哪儿产品评分
					//满足运营产品两两评分报表统计需求start
					saveProductGrade(lmmQnrTpg,Company.LMMQNR.getCode(),LMMProduct.getCommodityNum(),LMMProduct.getProductId()
							,LMMProduct.getTaskId(),qnrTpg,Company.QNR.getCode(),
							qnrCommodityNum,qnrProduct.getProduct_id(),qnrProduct.getTask_id());
					//满足运营产品两两评分报表统计需求end
				}
				if (mtTimes > 0) {
					double commodityGrade6 = CommodityGradeUtil.getObjectGrade(configManager, mtTimes,
							mtCommItem.getPriceGrade(), mtCommItem.getRefundCondGrade(),
							mtCommItem.getPreferentialGrade(), mtCommItem.getEfficientGrade(),
							mtCommItem.getBooktimeGrade());
					double productGrade = CommodityGradeUtil.getProductGrade(configManager, commodityGrade6, mtProduct.getCommodity_num(), Double.parseDouble(mtProduct.getImage_num()), mtProduct.getScore(), "MT");
					mtProduct.setProduct_grade(StringTools.formatDouble2(productGrade));
					mtProduct.setPriceGrade(String.valueOf(mtCommItem.getPriceGrade() / mtTimes));
					mtProduct.setRefundCondGrade(String.valueOf(mtCommItem.getRefundCondGrade() / mtTimes));
					mtProduct.setPreferentialGrade(String.valueOf(mtCommItem.getPreferentialGrade() / mtTimes));
					mtProduct.setEfficientGrade(String.valueOf(mtCommItem.getEfficientGrade() / mtTimes));
					mtProduct.setBooktimeGrade(String.valueOf(mtCommItem.getBooktimeGrade() / mtTimes));
					mtProductItemsService.updateByItem(mtProduct);
					String currenrKey = RowkeyGenerator.generateRowkey(mtProduct.getProduct_id(), mtProduct.getNew_grab_time(), mtProduct.getTask_id());
					mtProduct.setId(currenrKey);
					mtHbase.saveMTTicketProduct(mtProduct, Constant.TICKET_PRODUCT_H_MT,
							Constant.HB_VARIABLE_FAMILY);
					//累加产品评分
					mtProductTotalGrade+=productGrade;
					//参与评分计算的产品数累加
					mtProductNum++;

					// 保存去哪儿产品评分
					//满足运营产品两两评分报表统计需求start
					saveProductGrade(lmmMtTpg,Company.LMMMT.getCode(),LMMProduct.getCommodityNum(),LMMProduct.getProductId()
							,LMMProduct.getTaskId(),mtTpg,Company.MT.getCode(),
							mtCommodityNum,mtProduct.getProduct_id(),mtProduct.getTask_id());
					//满足运营产品两两评分报表统计需求end
				}
				// 产品级别评分计算、更新产品评分 end
			} catch (Exception e) {
				logger.error("门票评分流程ERROR>>>" + item.toString());
				logger.error("门票评分流程ERROR MESSAGE>>>" + e.getMessage());
			}
		}
		//用来统计各公司每次评分的横向平均分值
		List<ProductGrade> productGradeList = new ArrayList<>();
		try {
			ProductGrade lmm = generateProductGradeItem(lmmProductTotalGrade,lmmProductNum,Constant.LMM_C_CODE,Constant.TICKE_BUS_TYPE);
			ProductGrade qnr = generateProductGradeItem(qnrProductTotalGrade,qnrProductNum,Constant.QNR_C_CODE,Constant.TICKE_BUS_TYPE);
			ProductGrade tc = generateProductGradeItem(tcProductTotalGrade,tcProductNum,Constant.TC_C_CODE,Constant.TICKE_BUS_TYPE);
			ProductGrade tn = generateProductGradeItem(tnProductTotalGrade,tnProductNum,Constant.TN_C_CODE,Constant.TICKE_BUS_TYPE);
			ProductGrade xc = generateProductGradeItem(xcProductTotalGrade,xcProductNum,Constant.XC_C_CODE,Constant.TICKE_BUS_TYPE);
			ProductGrade mt = generateProductGradeItem(mtProductTotalGrade,mtProductNum,Constant.MT_C_CODE,Constant.TICKE_BUS_TYPE);
			if(lmm!=null){
				productGradeList.add(lmm);
			}
			if(qnr!=null){
				productGradeList.add(qnr);
			}
			if(tc!=null){
				productGradeList.add(tc);
			}
			if(tn!=null){
				productGradeList.add(tn);
			}
			if(xc!=null){
				productGradeList.add(xc);
			}
			if(mt!=null){
				productGradeList.add(mt);
			}
			if(productGradeList.size()>0){
				productGradeService.saveList(productGradeList);
			}
		} catch (Exception e) {
			logger.error("各平台门票产品平均分入库异常");
		}
		logger.info("门票产品评分处理完毕！产品关联个数："+relevanceProductList.size()+"个，各平台参与评分产品个数为：驴妈妈评分产品"+lmmProductNum+"个,驴妈妈未评分产品"+lmmFailedProductNum+"，去哪儿"+qnrProductNum+"个，携程"+xcProductNum+"个，同程"+tcProductNum+"个，途牛"+tnProductNum+"个，美团"+mtProductNum+"个。");
		relevanceProductList.clear();
	}

	/**
	 * 保存产品评分报表对象
	*/
	private void saveProductGrade(TicketProductGrade lmmTcTpg,  String lmmCode, int lmmCommodityNum,String lmmProductId,
			String lmmTaskId,TicketProductGrade subTpg,String subCode , int subCommodityNum, 
			String subProductId,  String subTaskId) {
		//驴妈妈对象
		lmmTcTpg.setProductId(lmmProductId);
		lmmTcTpg.setCompanyCode(lmmCode);
		lmmTcTpg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
		lmmTcTpg.setTaskId(lmmTaskId);
		lmmTcTpg.setPriceGrade(lmmTcTpg.getPriceGrade()/lmmTcTpg.getCommodityNum());
		lmmTcTpg.setPreferentialGrade(lmmTcTpg.getPreferentialGrade()/lmmTcTpg.getCommodityNum());
		lmmTcTpg.setBooktimeGrade(lmmTcTpg.getBooktimeGrade()/lmmTcTpg.getCommodityNum());
		lmmTcTpg.setEfficientGrade(lmmTcTpg.getEfficientGrade()/lmmTcTpg.getCommodityNum());
		lmmTcTpg.setRefundCondGrade(lmmTcTpg.getRefundCondGrade()/lmmTcTpg.getCommodityNum());
		lmmTcTpg.setCommodityGrade(CommodityGradeUtil.getCommodityGrade(configManager,lmmTcTpg));
		lmmTcTpg.setSkuGrade(CommodityGradeUtil.getSKUGrade(lmmCommodityNum, subCommodityNum));
		lmmTcTpg.setTotalGrade(CommodityGradeUtil.getTotalGrade(configManager,lmmTcTpg.getCommodityGrade(), lmmTcTpg.getSkuGrade()));
		int result = ticketProductGradeService.deleteSameObject(lmmTcTpg);
		ticketProductGradeService.insert(lmmTcTpg);
		//竞品对象
		subTpg.setProductId(subProductId);
		subTpg.setCompanyCode(subCode);
		subTpg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
		subTpg.setTaskId(subTaskId);
		subTpg.setPriceGrade(subTpg.getPriceGrade()/subTpg.getCommodityNum());
		subTpg.setPreferentialGrade(subTpg.getPreferentialGrade()/subTpg.getCommodityNum());
		subTpg.setBooktimeGrade(subTpg.getBooktimeGrade()/subTpg.getCommodityNum());
		subTpg.setEfficientGrade(subTpg.getEfficientGrade()/subTpg.getCommodityNum());
		subTpg.setRefundCondGrade(subTpg.getRefundCondGrade()/subTpg.getCommodityNum());
		subTpg.setCommodityGrade(CommodityGradeUtil.getCommodityGrade(configManager,subTpg));
		//这里注意顺序
		subTpg.setSkuGrade(CommodityGradeUtil.getSKUGrade(subCommodityNum, lmmCommodityNum));
		subTpg.setTotalGrade(CommodityGradeUtil.getTotalGrade(configManager,subTpg.getCommodityGrade(), subTpg.getSkuGrade()));
		ticketProductGradeService.deleteSameObject(subTpg);
		ticketProductGradeService.insert(subTpg);
	}

	/**
	* 当驴妈妈所关联的商品均无效时返回false
	*/
	private boolean preHandelCommodityValiable(XCTicketCommodityItem xcComm, TCommodityItem tcComm,
			TNTicketCommodityItem tnComm, QNRTicketCommodityItem qnrComm,MTTicketCommodityItem mtComm, QNRTicketProductItem qnrProduct,
			TNTicketProductItem tnProduct, TCProductItem tcProduct, XCTicketProductItem xcProduct,MTTicketProductItem mtProduct) {
		if (StringTools.isValid(xcComm.getSale_price()) && xcProduct!=null) {
			return true;
		}
		if (StringTools.isValid(tcComm.getSale_price()) && tcProduct!=null) {
			return true;
		}
		if (StringTools.isValid(tnComm.getSale_price()) && tnProduct!=null) {
			return true;
		}
		if (StringTools.isValid(qnrComm.getSale_price()) && qnrProduct!=null) {
			return true;
		}
		if (StringTools.isValid(mtComm.getSale_price()) && mtProduct!=null) {
			return true;
		}
		return false;
	}

	private ProductGrade generateProductGradeItem(double productTotalGrade, int productNum, String companyCode,
			String businessType) {
		Map<String,String> paramMap = new HashMap<>();
		paramMap.put("createDate", DateUtil.getCurrentymd(DateUtil.getDateBeforeToday(new Date())));
		paramMap.put("companyCode", companyCode);
		paramMap.put("businessType", businessType);
		//计算产品平均得分
		String productGradeStr = "0";
		try {
			if(productNum>0){
				productGradeStr = StringTools.formatDouble2(productTotalGrade/productNum);
				if(!StringTools.isDoubleNumeric(productGradeStr)){
					productGradeStr = "0";
				}
			}			
		} catch (Exception e) {
			productGradeStr = "0";
		}
		ProductGrade productGrade = new ProductGrade();
		productGrade.setProductGrade(productGradeStr);
		productGrade.setCreateDate(DateUtil.getDateBeforeToday(new Date()));
		productGrade.setCompanyCode(companyCode);
		productGrade.setBusinessType(businessType);
		List<ProductGrade> existItem = productGradeService.findListByMap(paramMap);
		
		//当天的只有一条数据
		if(existItem.size()<=0){
			return productGrade;
		}
		return null;
		
	}

	public void preHandelCommodity(ConfigManager configManager, LMMTicketCommodity lmmComm,
			XCTicketCommodityItem xcComm, TCommodityItem tcComm, TNTicketCommodityItem tnComm,
			QNRTicketCommodityItem qnrComm,MTTicketCommodityItem mtComm) {
		try {
			// 退改规则数据预处理
			double lmmrefundValue = CommodityGradeUtil.handelRefund(configManager, lmmComm.getRefundCond());
			lmmComm.setRefundCondValue(String.valueOf(lmmrefundValue));
			// 优惠数据预处理
			double lmmPreferentialValue = CommodityGradeUtil.handelPreferential(configManager,
					lmmComm.getPreferentialInfo());
			lmmComm.setPreferentialValue(String.valueOf(lmmPreferentialValue));
			// 预定时间数据预处理
			double lmmBooktimeValue = CommodityGradeUtil.handelBooktime(configManager, lmmComm.getSaleCond());
			lmmComm.setBooktimeValue(String.valueOf(lmmBooktimeValue));
			// 考虑生效时间大都没有该字段，统一赋值0
			lmmComm.setEfficientValue(String.valueOf(0));
			// 这边判断价格实际上也是判断当前商品是否有被关联，new的对象价格为null
			if (StringTools.isValid(xcComm.getSale_price())) {
				// 退改规则数据预处理
				double refundValue = CommodityGradeUtil.handelRefund(configManager, xcComm.getRefund_cond());
				xcComm.setRefundCondValue(String.valueOf(refundValue));
				double preferentialValue = CommodityGradeUtil.handelPreferential(configManager, xcComm.getPreferential());
				xcComm.setPreferentialValue(String.valueOf(preferentialValue));
				double booktimeValue = CommodityGradeUtil.handelBooktime(configManager, xcComm.getSale_cond());
				xcComm.setBooktimeValue(String.valueOf(booktimeValue));
				// 考虑生效时间大都没有该字段，统一赋值0
				xcComm.setEfficientValue(String.valueOf(0));
			}
			if (StringTools.isValid(tcComm.getSale_price())) {
				double tcrefundValue = CommodityGradeUtil.handelRefund(configManager, tcComm.getRefund_cond());
				tcComm.setRefundCondValue(String.valueOf(tcrefundValue));
				double preferentialValue = CommodityGradeUtil.handelPreferential(configManager, tcComm.getPreferential());
				tcComm.setPreferentialValue(String.valueOf(preferentialValue));
				double booktimeValue = CommodityGradeUtil.handelBooktime(configManager, tcComm.getSale_cond());
				tcComm.setBooktimeValue(String.valueOf(booktimeValue));
				tcComm.setEfficientValue(String.valueOf(0));
			}
			if (StringTools.isValid(tnComm.getSale_price())) {
				double tnrefundValue = CommodityGradeUtil.handelRefund(configManager, tnComm.getRefund_cond());
				tnComm.setRefundCondValue(String.valueOf(tnrefundValue));
				double preferentialValue = CommodityGradeUtil.handelPreferential(configManager, tnComm.getPreferential());
				tnComm.setPreferentialValue(String.valueOf(preferentialValue));
				double booktimeValue = CommodityGradeUtil.handelBooktime(configManager, tnComm.getSale_cond());
				tnComm.setBooktimeValue(String.valueOf(booktimeValue));
				tnComm.setEfficientValue(String.valueOf(0));
			}
			if (StringTools.isValid(qnrComm.getSale_price())) {
				double qnrrefundValue = CommodityGradeUtil.handelRefund(configManager, qnrComm.getRefund_cond());
				qnrComm.setRefundCondValue(String.valueOf(qnrrefundValue));
				double preferentialValue = CommodityGradeUtil.handelPreferential(configManager, qnrComm.getPreferential());
				qnrComm.setPreferentialValue(String.valueOf(preferentialValue));
				double booktimeValue = CommodityGradeUtil.handelBooktime(configManager, qnrComm.getSale_cond());
				qnrComm.setBooktimeValue(String.valueOf(booktimeValue));
				qnrComm.setEfficientValue(String.valueOf(0));
			}
			if (StringTools.isValid(mtComm.getSale_price())) {
				double mtrefundValue = CommodityGradeUtil.handelRefund(configManager, mtComm.getRefund_cond());
				mtComm.setRefundCondValue(String.valueOf(mtrefundValue));
				double preferentialValue = CommodityGradeUtil.handelPreferential(configManager, mtComm.getPreferential());
				mtComm.setPreferentialValue(String.valueOf(preferentialValue));
				double booktimeValue = CommodityGradeUtil.handelBooktime(configManager, mtComm.getSale_cond());
				mtComm.setBooktimeValue(String.valueOf(booktimeValue));
				mtComm.setEfficientValue(String.valueOf(0));
			}

		} catch (Exception e) {
			logger.info("门票产品评分异常："+e.getMessage());
		}
		
	}
	
	public void saveTicketCommodityGrade(LMMTicketCommodity lmmComm, XCTicketCommodityItem xcComm,
			TCommodityItem tcComm, TNTicketCommodityItem tnComm, QNRTicketCommodityItem qnrComm,MTTicketCommodityItem mtComm) {

		TicketCommodityGrade lmmTcg = new TicketCommodityGrade();
		// 保存驴妈妈商品评分
		lmmTcg.setProductId(lmmComm.getProductId());
		lmmTcg.setCommodityId(lmmComm.getCommodityId());
		lmmTcg.setCommodityName(lmmComm.getCommodityName());
		lmmTcg.setCompanyCode(Company.LMM.getCode());
		lmmTcg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
		lmmTcg.setTaskId(lmmComm.getTaskId());
		lmmTcg.setSalePrice(Double.valueOf(lmmComm.getSalePrice()));
		lmmTcg.setPreferentialInfo(lmmComm.getPreferentialInfo());
		lmmTcg.setPreferentialValue(Double.valueOf(lmmComm.getPreferentialValue()));
		lmmTcg.setRefundCond(lmmComm.getRefundCond());
		lmmTcg.setRefundCondValue(Double.valueOf(lmmComm.getRefundCondValue()));
		lmmTcg.setBookTime(lmmComm.getSaleCond());
		lmmTcg.setBookTimeValue(Double.valueOf(lmmComm.getBooktimeValue()));
		lmmTcg.setEfficientTime(null);// 暂无生效时间字段
		lmmTcg.setEfficientTimeValue(Double.valueOf(lmmComm.getEfficientValue()));
//		int result = ticketCommodityGradeService.deleteSameObject(lmmTcg);
		ticketCommodityGradeService.insert(lmmTcg);

		// 保存携程商品评分
		if (StringTools.isValid(xcComm.getSale_price())) {
			TicketCommodityGrade xcTcg = new TicketCommodityGrade();
			xcTcg.setProductId(xcComm.getProduct_id());
			xcTcg.setCommodityId(xcComm.getCommodity_id());
			xcTcg.setCommodityName(xcComm.getCommodity_name());
			xcTcg.setCompanyCode(Company.XC.getCode());
			xcTcg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
			xcTcg.setTaskId(xcComm.getTask_id());
			xcTcg.setSalePrice(Double.valueOf(xcComm.getSale_price()));
			xcTcg.setPreferentialInfo(xcComm.getPreferential());
			xcTcg.setPreferentialValue(Double.valueOf(xcComm.getPreferentialValue()));
			xcTcg.setRefundCond(xcComm.getRefund_cond());
			xcTcg.setRefundCondValue(Double.valueOf(xcComm.getRefundCondValue()));
			xcTcg.setBookTime(xcComm.getSale_cond());
			xcTcg.setBookTimeValue(Double.valueOf(xcComm.getBooktimeValue()));
			xcTcg.setEfficientTime(null);// 暂无生效时间字段
			xcTcg.setEfficientTimeValue(Double.valueOf(xcComm.getEfficientValue()));
//			ticketCommodityGradeService.deleteSameObject(xcTcg);
			ticketCommodityGradeService.insert(xcTcg);
		}

		// 保存同程商品评分
		if (StringTools.isValid(tcComm.getSale_price())) {
			TicketCommodityGrade tcTcg = new TicketCommodityGrade();
			tcTcg.setProductId(tcComm.getProduct_id());
			tcTcg.setCommodityId(tcComm.getCommodity_id());
			tcTcg.setCommodityName(tcComm.getCommodity_name());
			tcTcg.setCompanyCode(Company.TC.getCode());
			tcTcg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
			tcTcg.setTaskId(tcComm.getTask_id());
			tcTcg.setSalePrice(Double.valueOf(tcComm.getSale_price()));
			tcTcg.setPreferentialInfo(tcComm.getPreferential());
			tcTcg.setPreferentialValue(Double.valueOf(tcComm.getPreferentialValue()));
			tcTcg.setRefundCond(tcComm.getRefund_cond());
			tcTcg.setRefundCondValue(Double.valueOf(tcComm.getRefundCondValue()));
			tcTcg.setBookTime(tcComm.getSale_cond());
			tcTcg.setBookTimeValue(Double.valueOf(tcComm.getBooktimeValue()));
			tcTcg.setEfficientTime(null);// 暂无生效时间字段
			tcTcg.setEfficientTimeValue(Double.valueOf(tcComm.getEfficientValue()));
//			ticketCommodityGradeService.deleteSameObject(tcTcg);
			ticketCommodityGradeService.insert(tcTcg);
		}

		// 保存途牛商品评分
		if (StringTools.isValid(tnComm.getSale_price())) {
			TicketCommodityGrade tnTcg = new TicketCommodityGrade();
			tnTcg.setProductId(tnComm.getProduct_id());
			tnTcg.setCommodityId(tnComm.getCommodity_id());
			tnTcg.setCommodityName(tnComm.getCommodity_name());
			tnTcg.setCompanyCode(Company.TN.getCode());
			tnTcg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
			tnTcg.setTaskId(tnComm.getTask_id());
			tnTcg.setSalePrice(Double.valueOf(tnComm.getSale_price()));
			tnTcg.setPreferentialInfo(tnComm.getPreferential());
			tnTcg.setPreferentialValue(Double.valueOf(tnComm.getPreferentialValue()));
			tnTcg.setRefundCond(tnComm.getRefund_cond());
			tnTcg.setRefundCondValue(Double.valueOf(tnComm.getRefundCondValue()));
			tnTcg.setBookTime(tnComm.getSale_cond());
			tnTcg.setBookTimeValue(Double.valueOf(tnComm.getBooktimeValue()));
			tnTcg.setEfficientTime(null);// 暂无生效时间字段
			tnTcg.setEfficientTimeValue(Double.valueOf(tnComm.getEfficientValue()));
//			ticketCommodityGradeService.deleteSameObject(tnTcg);
			ticketCommodityGradeService.insert(tnTcg);
		}

		// 保存去哪儿商品评分
		if (StringTools.isValid(qnrComm.getSale_price())) {
			TicketCommodityGrade qnrTcg = new TicketCommodityGrade();
			qnrTcg.setProductId(qnrComm.getProduct_id());
			qnrTcg.setCommodityId(qnrComm.getCommodity_id());
			qnrTcg.setCommodityName(qnrComm.getCommodity_name());
			qnrTcg.setCompanyCode(Company.QNR.getCode());
			qnrTcg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
			qnrTcg.setTaskId(qnrComm.getTask_id());
			qnrTcg.setSalePrice(Double.valueOf(qnrComm.getSale_price()));
			qnrTcg.setPreferentialInfo(qnrComm.getPreferential());
			qnrTcg.setPreferentialValue(Double.valueOf(qnrComm.getPreferentialValue()));
			qnrTcg.setRefundCond(qnrComm.getRefund_cond());
			qnrTcg.setRefundCondValue(Double.valueOf(qnrComm.getRefundCondValue()));
			qnrTcg.setBookTime(qnrComm.getSale_cond());
			qnrTcg.setBookTimeValue(Double.valueOf(qnrComm.getBooktimeValue()));
			qnrTcg.setEfficientTime(null);// 暂无生效时间字段
			qnrTcg.setEfficientTimeValue(Double.valueOf(qnrComm.getEfficientValue()));
//			ticketCommodityGradeService.deleteSameObject(qnrTcg);
			ticketCommodityGradeService.insert(qnrTcg);
		}
		//
		if (StringTools.isValid(mtComm.getSale_price())) {
			TicketCommodityGrade mtTcg = new TicketCommodityGrade();
			mtTcg.setProductId(mtComm.getProduct_id());
			mtTcg.setCommodityId(mtComm.getCommodity_id());
			mtTcg.setCommodityName(mtComm.getCommodity_name());
			mtTcg.setCompanyCode(Company.MT.getCode());
			mtTcg.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));
			mtTcg.setTaskId(mtComm.getTask_id());
			mtTcg.setSalePrice(Double.valueOf(mtComm.getSale_price()));
			mtTcg.setPreferentialInfo(mtComm.getPreferential());
			mtTcg.setPreferentialValue(Double.valueOf(mtComm.getPreferentialValue()));
			mtTcg.setRefundCond(mtComm.getRefund_cond());
			mtTcg.setRefundCondValue(Double.valueOf(mtComm.getRefundCondValue()));
			mtTcg.setBookTime(mtComm.getSale_cond());
			mtTcg.setBookTimeValue(Double.valueOf(mtComm.getBooktimeValue()));
			mtTcg.setEfficientTime(null);// 暂无生效时间字段
			mtTcg.setEfficientTimeValue(Double.valueOf(mtComm.getEfficientValue()));
//			ticketCommodityGradeService.deleteSameObject(mtTcg);
			ticketCommodityGradeService.insert(mtTcg);
		}


	}
}
